home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8309 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.eden.com!usenet
  2. From: Shane Sadler <nexus@eden.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Checking for Ilegal Input
  5. Date: Sat, 02 Mar 1996 20:11:03 -0700
  6. Organization: Eden Matrix Services
  7. Message-ID: <31390DC7.DB9DB49@eden.com>
  8. References: <313855C7.5E86@aol.com>
  9. NNTP-Posting-Host: net-4-139.austin.eden.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
  14.  
  15. gregace@aol.com wrote:
  16. > I have a program where you must enter a number between 0 and 15 and check for
  17. > illegal input such as a number not between 0 and 15 or if a character is
  18. > entered.
  19. > I used the scanf("%d",&number) to read the input.
  20. > I check the illegal input by
  21. > while (number<0 or number>15)
  22. > { Printf("Enter a again:");
  23. >   scanf("%d",&number);
  24. > }
  25. > This check works fine accept when the user enters a character such as the
  26. > letter f. Does anyone know how to check illegal input that is not a number?
  27.  
  28. Declare a flag that accepts a return value from scanf(). That function
  29. returns a zero if there was input, but no conversions assigned. That's
  30. one way to check for invalid input (someone entering "foo" when you're
  31. looking for a %d conversion). The same holds true for the entire scanf()
  32. family.
  33.  
  34. Hope this helps.
  35.  
  36. -- Shane
  37. ===================================================================
  38. S. M. Sadler
  39. e-mail: nexus@eden.com
  40. Web: http://www.eden.com/~nexus
  41.